<?php

/**
 * A set of useful collections algorithms
 *
 * @author JeyDotC
 */
class Collections {
    
    public static function intersect($array1, $array2, $equalsComparator) {
        $resultArray = array();
        foreach ($array1 as $key => $masterValue) {
            foreach ($array2 as $value) {
                if(self::equals($masterValue, $value, $equalsComparator)){
                    $resultArray[$key] = $masterValue;
                    break;
                }
            }
        }
        
        return $resultArray;
    }
    
    public static function difference($array1, $array2, $equalsComparator) {
        $resultArray = array();
        foreach ($array1 as $key => $masterValue) {
            foreach ($array2 as $value) {
                if(!self::equals($masterValue, $value, $equalsComparator)){
                    $resultArray[$key] = $masterValue;
                    break;
                }
            }
        }
        
        return $resultArray;
    }


    private static function equals($object1, $object2, $comparator) {
        $equals = false;
        if(is_callable($comparator)){
            $equals = call_user_func($comparator, $object1, $object2);
        }else{
            $equals = $object1 == $object2;
        }
        
        return $equals;
    }
}

?>
